<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent
      >
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="游戏名称:">
              <el-input
                :placeholder="'请输入游戏名称'"
                v-model="where.game_name"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :lg="6" :md="12">
            <el-form-item label="状态:">
              <el-select
                clearable
                v-model="where.status"
                placeholder="请选择状态"
                class="ele-fluid"
              >
                <el-option label="全部" value="" />
                <el-option label="启用" value="1" />
                <el-option label="禁用" value="2" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="投放设备:">
              <el-select
                clearable
                v-model="where.device"
                placeholder="请选择投放设备"
                class="ele-fluid"
              >
                <el-option label="全部" value="0" />
                <el-option label="安卓" value="2" />
                <el-option label="IOS" value="1" />
                <el-option label="PC" value="3" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload"
                >查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)"
      >
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:open:add')"
            >添加
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
            v-if="permission.includes('sys:open:dall')"
            >删除
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="permission.includes('sys:open:edit')"
            >修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此开服吗？"
            @confirm="remove(row)"
          >
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              v-if="permission.includes('sys:open:delete')"
              >删除
            </el-link>
          </el-popconfirm>
        </template>
        <!-- 投放平台列 -->
        <template slot="platform" slot-scope="{ row }">
          <el-tag v-if="row.platform === 1" type="success" size="small"
            >PC站</el-tag
          >
          <el-tag v-if="row.platform === 2" type="warning" size="small"
            >WAP站</el-tag
          >
          <el-tag v-if="row.platform === 3" type="danger" size="small"
            >微信小程序</el-tag
          >
          <el-tag v-if="row.platform === 4" type="info" size="small"
            >APP应用</el-tag
          >
        </template>
        <!-- 状态列 -->
        <template slot="status" slot-scope="{ row }">
          <el-switch
            v-model="row.status"
            @change="editStatus(row)"
            :active-value="1"
            :inactive-value="2"
          />
        </template>
        <!-- 头像 -->
        <!-- <template slot="game_info" slot-scope="{row}">
          <div>
            <div class="game-info">
              <div class="game-img">
                <el-avatar shape="square" :size="25" :src="row.game_info.icon"/>
              </div>
              <div class="game-code" >
                游戏编码：{{row.game_info.id}}
               </div>
              <div class="game-name" >
                游戏名称：{{row.game_info.game_name}}
              </div>
             </div>
          </div>
        </template> -->
        <template slot="game_info" slot-scope="{ row }">
          {{ row.game_name }}({{ row.game_id }})
        </template>

        <!--        <template slot="game_info_icon" slot-scope="{ row }">-->
        <!--          <el-avatar shape="square" :size="25" :src="row.icon" />-->
        <!--        </template>-->

        <template slot="game_info_icon" slot-scope="{ row }">
          <el-image
            v-if="row.icon == '' || row.icon == null"
            style="width: 28px; height: 28px"
          >
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
          <el-image
            v-else
            style="height: 28px; width: auto; border-radius: 8px"
            :src="row.icon"
            :preview-src-list="[row.icon]"
          />
        </template>
        <template slot="media" slot-scope="{ row }">
          <el-image
            v-if="
              row.media[0] === '' ||
              row.media[0] === null ||
              row.media[0] === undefined
            "
            style="width: 28px; height: 28px"
          >
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
          <!--          <video-->
          <!--            v-if="row.media[0].type === 'mp4'"-->
          <!--            controls-->
          <!--            style="width: 100%; height: auto"-->
          <!--          >-->
          <!--            <source :src="row.media[0].data" type="video/mp4" />-->
          <!--          </video>-->

          <el-image
            v-else
            style="height: 28px; width: auto; border-radius: 8px"
            :src="row.media[0].img"
            :preview-src-list="[row.media[0].img]"
          />
        </template>
        <template slot="icons" slot-scope="{ row }">
          <el-image
            v-if="row.icons == '' || row.icons == null"
            style="width: 28px; height: 28px"
          >
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
          <el-image
            v-else
            style="height: 28px; width: auto; border-radius: 8px"
            :src="row.icons"
            :preview-src-list="[row.icons]"
          />
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <edit
      :data="current"
      :visible.sync="showEdit"
      @done="reload"
      :key="someUniqueKey()"
    />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import edit from "./edit";

export default {
  name: "SystemLink",
  components: { edit },
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: "/BoxGameOpen/index",
      // 表格列配置
      columns: [
        {
          columnKey: "selection",
          type: "selection",
          width: 45,
          align: "center",
          fixed: "left",
        },
        {
          prop: "id",
          label: "ID",
          width: 60,
          align: "center",
          showOverflowTooltip: true,
        },
        // {
        //   prop: 'game_id',
        //   label: '游戏ID',
        //   align: 'center',
        //   showOverflowTooltip: true,
        //   minWidth: 60,
        // },
        {
          prop: "game_name",
          label: "游戏名称",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 180,
          slot: "game_info",
        },
        {
          prop: "icon",
          label: "游戏icon",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 120,
          slot: "game_info_icon",
        },
        {
          prop: "media",
          label: "宣传视频",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 120,
          slot: "media",
        },
        {
          prop: "icons",
          label: "宣传图",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 120,
          slot: "icons",
        },

        {
          prop: "server_id",
          label: "区服ID",
          showOverflowTooltip: true,
          minWidth: 120,
          align: "center",
        },
        {
          prop: "name",
          label: "区服名称",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center",
        },
        {
          prop: "status",
          label: "区服状态",
          align: "center",
          width: 100,
          resizable: false,
          slot: "status",
        },
        {
          prop: "desc",
          label: "区服介绍",
          align: "center",
          width: 200,
          showOverflowTooltip: true,
        },
        {
          prop: "open_time",
          label: "开服时间",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          },
        },
        {
          prop: "create_time",
          label: "创建时间",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          },
        },

        {
          columnKey: "action",
          label: "操作",
          width: 150,
          align: "center",
          resizable: false,
          slot: "action",
          fixed: "right",
        },
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
    };
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      this.$http
        .post("/BoxGameOpen/delete", { id: row.id })
        .then((res) => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error("请至少选择一条数据");
        return;
      }
      this.$confirm("确定要删除选中的友链吗?", "提示", {
        type: "warning",
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          this.$http
            .post("/BoxGameOpen/delete", {
              id: this.selection.map((d) => d.id),
            })
            .then((res) => {
              loading.close();
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                this.reload();
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },
    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({ lock: true });
      let params = Object.assign({
        id: row.id,
        status: row.status,
      });
      this.$http
        .post("/BoxGameOpen/status", params)
        .then((res) => {
          loading.close();
          if (res.data.code === 0) {
            this.$message({ type: "success", message: res.data.msg });
          } else {
            row.status = !row.status ? 1 : 2;
            this.$message.error(res.data.msg);
          }
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 唯一key */
    someUniqueKey() {
      return Math.random().toString(36).substr(3, 10);
    },
  },
};
</script>

<style scoped></style>
